import React, { PureComponent } from 'react';
import { Form, Input,Select} from 'antd';
import { connect } from 'dva';
class StructureForm extends PureComponent {
    constructor(props) {
        super(props);
        this.state = {}
        this.onConfirm = this.onConfirm.bind(this);
    }
    componentWillMount(){
        if(this.props.updateStructures){
            this.props.updateStructures({})
        }
    }
    componentDidMount() {
        if (this.props.onLoaded) {
            this.props.onLoaded(this);
        }
    }

    onConfirm(params = {}) {
        this.props.form.validateFields((error, values) => {
            if (!error) {
                this.props.form.resetFields()
                this.props.refStructure(values, (structure) => {
                    if (params.onSuccess) {
                        params.onSuccess(structure);
                    }
                }, (error) => {
                    if (params.onError) {
                        params.onError(error);
                    }
                });
            } else {
                if (params.onError) {
                    params.onError(error);
                }
            }
        });
    }

    render() {
        const {
            form = {},
            projectId,
            structures,
        } = this.props;
        const { getFieldDecorator } = form;
        return (
            <Form
                labelCol={{ span: 6 }}
                wrapperCol={{ span: 18 }}
            >
                <Form.Item 
                    label='ID'
                    style={{ display: 'none' }}>
                    {getFieldDecorator('projectId', {
                        rules: [
                            { required:true, message: '必填' }
                        ],
                        initialValue:projectId
                    })(
                        <Input />
                    )}
                </Form.Item>
                <Form.Item label='构件'>
                    {getFieldDecorator('definitionIds', {
                        rules: [
                            { required: true, message: '必填' }
                        ]
                    })(
                        <Select mode='multiple' placeholder="请输入搜索关键字">
                            {structures && structures.length > 0 ? structures.map((r, i) => (
                                <Select.Option key={r.id} value={r.id}>{r.name}</Select.Option>
                            )) : ''}
                        </Select>
                    )}
                </Form.Item>
            </Form>
        );
    }
}

export default connect(
    ({
        structure: { designedStructures},
    }) => {
        return { structures };
    },
    dispatch => ({
        refStructure: (values, onSuccess, onError) => dispatch({
            type: 'structure/ref',
            payload: {
                values,
                onSuccess,
                onError

            }
        }),
        updateStructures: (values) => dispatch({
            type: 'structure/updateStructures',
            payload: values
        })
    })
)(Form.create('StructureForm', {

})(StructureForm));